/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.managedbean;

import au.edu.swin.swinvehicle.corporateclient.CorporateClientManagerException;
import au.edu.swin.swinvehicle.corporateclient.CorporateClientManagerRemote;
import au.edu.swin.swinvehicle.corporateclient.CorporateClientTO;
import au.edu.swin.swinvehicle.driver.DriverManagerException;
import au.edu.swin.swinvehicle.driver.DriverManagerRemote;
import au.edu.swin.swinvehicle.driver.DriverTO;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerException;
import au.edu.swin.swinvehicle.vehicle.VehicleManagerRemote;
import au.edu.swin.swinvehicle.vehicle.VehicleTO;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingDetailedTO;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingFilterTO;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingManagerException;
import au.edu.swin.swinvehicle.vehiclebooking.VehicleBookingManagerRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author Administrator
 */
public class JobReportJSFManagedBean {

    public static final String JOB_REPORT = "jobReport";
    @EJB
    private VehicleBookingManagerRemote vehicleBookingManagerBean;
    @EJB
    private VehicleManagerRemote vehicleManagerBean;
    @EJB
    private DriverManagerRemote driverManagerBean;
    @EJB
    private CorporateClientManagerRemote corporateClientManagerBean;
    private List<VehicleBookingDetailedTO> vehicleBookings;
    private VehicleBookingFilterTO filter;

    /** Creates a new instance of JobReportJSFManagedBean */
    public JobReportJSFManagedBean() {
        this.filter = new VehicleBookingFilterTO();
        this.vehicleBookings = new ArrayList<VehicleBookingDetailedTO>();
    }

    public List<VehicleBookingDetailedTO> getVehicleBookings() {
        return vehicleBookings;
    }

    public void setVehicleBookings(List<VehicleBookingDetailedTO> vehicleBookings) {
        this.vehicleBookings = vehicleBookings;
    }

    public VehicleBookingFilterTO getFilter() {
        return filter;
    }

    public void setFilter(VehicleBookingFilterTO filter) {
        this.filter = filter;
    }

    public List<SelectItem> getVehicles() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select vehicle ---"));
        try {
            List<VehicleTO> vehicles = this.vehicleManagerBean.getAllVehicles();
            for (VehicleTO vehicle : vehicles) {
                selectItems.add(new SelectItem(vehicle.getId(), vehicle.getName()));
            }
        } catch (VehicleManagerException ex) {
            Logger.getLogger(JobReportJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return selectItems;
    }

    public List<SelectItem> getDrivers() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select driver ---"));
        try {
            List<DriverTO> drivers = this.driverManagerBean.getAllDrivers();
            for (DriverTO driver : drivers) {
                selectItems.add(new SelectItem(driver.getId(), driver.getUsername()));
            }
        } catch (DriverManagerException ex) {
            Logger.getLogger(JobReportJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return selectItems;
    }

    public List<SelectItem> getCorporateClients() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "--- Select corporate client ---"));
        try {
            List<CorporateClientTO> corporateClients = this.corporateClientManagerBean.getAllCorporateClients();
            for (CorporateClientTO corporateClient : corporateClients) {
                selectItems.add(new SelectItem(corporateClient.getId(), corporateClient.getUsername()));
            }
        } catch (CorporateClientManagerException ex) {
            Logger.getLogger(JobReportJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return selectItems;
    }

    public String filterJobReport() {
        try {
            this.vehicleBookings = this.vehicleBookingManagerBean.getDetailedVehicleBookingsByFilter(this.filter);
            return JOB_REPORT;
        } catch (VehicleBookingManagerException ex) {
            Logger.getLogger(JobReportJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot add filter job report. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralJobReportErrorNotification", msg);

            return JOB_REPORT;
        }
    }
}
